/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Conexiones;

/**
 *
 * @author
 */
import interfaces.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MakingTheConnection {
    static Connection databaseConnection;
    private String server;
    private String user;
    private String password;
    private String database;
    public MakingTheConnection() {
        connection("localhost", "root","", "laboratorio");
    }

    public void connection(String server, String user, String password, String database){
               try {
              // Load the driver class
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

              // Define the data source for the driver
              String sourceURL = "jdbc:mysql://"+server+"/"+database;//"jdbc:odbc:technical_library";

              // Create a connection through the DriverManager
              databaseConnection = DriverManager.getConnection(sourceURL,user,password);
              System.out.println("Connection is: "+databaseConnection);
              if(databaseConnection==null)System.out.println("no conectado");
        } catch(ClassNotFoundException cnfe) {
          System.err.println(cnfe);
        } catch(SQLException sqle) {
          System.err.println(sqle);
        }

    }

    public void closeConnection(){
        try {
            databaseConnection.close();
        } catch (SQLException ex) {
            Logger.getLogger(MakingTheConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static ResultSet query(String sql) throws SQLException{
        //try{
            Statement s = databaseConnection.createStatement();
            ResultSet rs = s.executeQuery (sql);
            return rs;
        //}catch(SQLException e){
        //    return null;

    }
    public static boolean update(String sql){
        try{
            Statement s = databaseConnection.createStatement();
            s.executeUpdate(sql);
            return true;
        }catch(SQLException e){
            System.out.println(e.getMessage());
            return false;
        }
    }
    
    public static boolean insert(String sql){
        try{
            Statement s = databaseConnection.createStatement();
            s.executeUpdate(sql);
            return true;
        }catch(SQLException e){
            System.out.println(e.getMessage());
            return false;
        }
    }
       public static boolean eliminar(String sql){
        try{
            Statement s = databaseConnection.createStatement();
            s.executeUpdate(sql);
            return true;
        }catch(SQLException e){
            System.out.println(e.getMessage());
            return false;
        }
    }


    public int getRows() throws SQLException{
        Statement s = databaseConnection.createStatement();
        ResultSet rs=s.executeQuery("SELECT * FROM `USUARIO`");
        int i=0;
        while(rs.next()){
            i++;
        }
        return i;
    }



}

